<?php
// Copyright 2007 Facebook Corp.  All Rights Reserved. 
// 
// Application: sampleTrust
// File: 'index.php' 
//   This is a sample skeleton for your application. 
// 

ini_set("display_errors", 0);

require_once 'facebook.php';
$appapikey = 'c55d6c8c312ca6c555b1003c8338bbb2';
$appsecret = '963ed6fbc428d7c746fd42ccd2bdf09d';
$facebook = new Facebook($appapikey, $appsecret);
$user_id = -1;
$facebook->require_login();
if($facebook->get_loggedin_user()){
    try{
    	$user_id = $facebook->require_login();
    }catch (Exception $ex) {
    	$facebook->clear_cookie_state();
    }
}

//$user_id = $facebook->require_login();
$user_info = $facebook->api_client->users_getInfo($user_id, "name, pic_square, pic, first_name, current_location");
$user_name = $user_info[0]["name"];
$user_pic = array("pic_square" => $user_info[0]["pic_square"], "pic" => $user_info[0]["pic"]);
$user_first_name = $user_info[0]["first_name"];
$user_current_location = $user_info[0]["current_location"]; 

function getUserFirstName(){
    global $user_first_name;
    return $user_first_name;
}

function getUserZipCode(){
    global $user_current_location;
    return $user_current_location["zip"];
}

function getFacebookAPIClient(){
	global $facebook;
	return $facebook;
}

function getUserPic(){
    global $user_pic;
    //echo $user_pic["pic_square"] .":". $user_pic["pic"];
    return $user_pic;
}

function getUserId(){
	global $user_id;
	return $user_id;
}

function getUserName(){
	global $user_name;
	return $user_name;
}

function getFriends(){
	global $facebook;
	$friends = $facebook->api_client->friends_get();
	return $friends;
}

function executeQuery($query){
	global $facebook;
	$result = $facebook->api_client->fql_query($query);
	echo $result;
	return $result;
}

function checkPermission(){
	global $facebook;
	$permission = $facebook->api_client->users_hasAppPermission("create_event");
	return $permission;
}

function newSharePost(){
  global $facebook;
  $message = 'Check out this cute pic.';
  $attachment = array(
      'name' => 'i\'m bursting with joy',
      'href' => 'http://icanhascheezburger.com/2009/04/22/funny-pictures-bursting-with-joy/',
      'caption' => '{*actor*} rated the lolcat 5 stars',
      'description' => 'a funny looking cat',
      'properties' => array('category' => array(
                            'text' => 'humor',
                            'href' => 'http://www.icanhascheezburger.com/category/humor'),
                            'ratings' => '5 stars'),
      'media' => array(array('type' => 'image',
                             'src' => 'http://icanhascheezburger.files.wordpress.com/2009/03/funny-pictures-your-cat-is-bursting-with-joy1.jpg',
                             'href' => 'http://icanhascheezburger.com/2009/04/22/funny-pictures-bursting-with-joy/')),
      'latitude' => '41.4',     //Let's add some custom metadata in the form of key/value pairs
      'longitude' => '2.19');
  $action_links = array(
                      array('text' => 'Recaption this',
                            'href' => 'http://mine.icanhascheezburger.com/default.aspx?tiid=1192742&recap=1#step2'));
  $attachment = json_encode($attachment);
  $action_links = json_encode($action_links);
  $facebook->api_client->stream_publish($message, $attachment, $action_links);	
}

function sharePost(){
  global $facebook;
  $one_line_story_templates =
    array('{*actor*} just bought {*album*} by {*artist*} from FBM.',
          '{*actor*} have been listening to a lot of {*artist*} at FBM.',
          '{*actor*} have been listening to a lot of their favorite artists at FBM.');

  $short_story_templates =
    array(array('template_title' => '{*actor*} recently bought {*album*} by {*artist*} from FBM.',
                'template_body' => '{*actor*} just bought {*album*} by {*artist*}, which includes '.
                '{*song*}, {*song2*}, and {*song3*}.'),
          array('template_title' => '{*actor*} have been listening to a lot of {*artist*} at FBM.',
                'template_body' => '{*actor*} have been listing to a lot of {*artist*} at FBM.  Click one of images '.
                'to listen to 30 seconds of the single, {*song*}.'));

  $full_story_template =
    array('template_title' => '{*actor*} just now bought {*album*} by {*artist*} from FBM.',
          'template_body' => 'Click below to watch the latest {*artist*} video, available only through FBM.');

  $template_bundle_id =
    $facebook->api_client->feed_registerTemplateBundle($one_line_story_templates,
                                                       $short_story_templates,
                                                       $full_story_template);
  global $user_id;
  $template_data =
    array('album' => 'Abbey Road',
          'artist' => 'The Beatles',
          'song' => 'Come Together',
          'song2' => 'Maxwell\'s Silver Hammer',
          'song3' => 'Here Comes The Sun',
          'images' => array(array('src' => 'http://ecx.images-amazon.com/images/I/41BMQS09H4L._SL500_AA240_.jpg',
                                  'href' => 'http://www.amazon.com'),
                            array('src' => 'http://ecx.images-amazon.com/images/I/41H2RGWMNAL._SL500_AA240_.jpg',
                                  'href' => 'http://www.amazon.com')));

  $success =
    $facebook->api_client->feed_publishUserAction($template_bundle_id,
                                                  $template_data);	
  echo $success;
}

function getFriendsUIDInApplication(){
    global $facebook;
	global $user_id;
	$fql = 'SELECT uid FROM user WHERE uid IN (SELECT uid2 FROM friend WHERE uid1='.$user_id.') AND has_added_app=1';
	$_friends = $facebook->api_client->fql_query($fql);

	// Extract the user ID's returned in the FQL request into a new array.
	$friends = array();
	if (is_array($_friends) && count($_friends)) {
        	foreach ($_friends as $friend) {
        	        $friends[] = $friend["uid"];
        	}
	}
    $friends = implode(',', $friends);
	return $friends;	
}

function getFriendsInApplication(){
	global $facebook;
	global $user_id;
	$fql = 'SELECT uid, first_name, name, pic_small, pic_square, pic FROM user WHERE uid IN (SELECT uid2 FROM friend WHERE uid1='.$user_id.') AND has_added_app=1';
	$_friends = $facebook->api_client->fql_query($fql);

	// Extract the user ID's returned in the FQL request into a new array.
	$friends = array();
	if (is_array($_friends) && count($_friends)) {
        	foreach ($_friends as $friend) {
        	        $friend_details = array("uid" => $friend["uid"], "name" => $friend["name"], "pic_square" => $friend["pic_square"], "first_name" => $friend["first_name"], "pic" => $friend["pic"]);
                	$friends[] = $friend_details;
        	}
	}

	// Convert the array of friends into a comma-delimeted string.
	return $friends;	
}

?>

